package com.moneywelcome.atomloanassembly.apiimpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moneywelcome.atomloanassembly.model.Amortization;
import com.moneywelcome.atomloanassembly.model.UserLoan;
import com.moneywelcome.atomloanassembly.service.AmotizationService;
import com.moneywelcome.atomloanassembly.service.UserLoanService;
import com.moneywelcome.atomloanfacade.api.UserLoanApi;
import com.moneywelcome.atomloanfacade.form.SaveLoanForm;
import com.moneywelcome.atomloanfacade.info.LoanInfo;
import com.moneywelcome.params.PageParam;
import com.moneywelcome.results.PageResult;
import com.moneywelcome.results.Result;
//import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

@RestController
public class UserLoanApiImpl implements UserLoanApi {
    @Autowired
    private UserLoanService userLoanService;

    @Autowired
    private AmotizationService amotizationService;

//    @GlobalTransactional
    @Override
    public Result saveLoan(SaveLoanForm form) {
        //存入贷款
        UserLoan userLoan = BeanUtil.copyProperties(form, UserLoan.class);
        boolean save = userLoanService.save(userLoan);
        //存入分期
        ArrayList<Amortization> list = new ArrayList<>();
        //每期应还(本金+利息)/期数
        Double repay = (form.getLoanMoney() + form.getLoanMoney() * (form.getLoanRate() / 12) * form.getLoanStage()) / form.getLoanStage();
        int year = DateUtil.thisYear();
        int thisMonth = DateUtil.thisMonth();
        Calendar calendar = Calendar.getInstance();
        calendar.set(year,thisMonth,15);
        for (int i=1;i<=form.getLoanStage();i++) {
            calendar.add(Calendar.MONTH,1);
            Amortization amortization = new Amortization();
            amortization.setUserId(form.getUserId());
            amortization.setStageNumber(i);
            amortization.setRepayMoney(repay);
            amortization.setLoanId(userLoan.getLoanId());
            amortization.setRepayTime(calendar.getTime());
            amortization.setRepayAccount(form.getBankAccount());
            list.add(amortization);
        }
        amotizationService.saveBatch(list);
        return save?Result.success():Result.fail();
    }

    @Override
    public Result<PageResult<LoanInfo>> getLoanByUser(Integer userId, PageParam param) {
        Page<UserLoan> loanPage = userLoanService.page(new Page<UserLoan>(param.getPageNumber(), param.getPageSize()), new QueryWrapper<UserLoan>().eq("user_id", userId));
        return Result.success(PageResult.getPageResult(loanPage));
    }

    @Override
    public Result<PageResult<LoanInfo>> getLoans(PageParam param) {
        Page<UserLoan> loanPage = userLoanService.page(new Page<UserLoan>(param.getPageNumber(), param.getPageSize()));
        return Result.success(PageResult.getPageResult(loanPage));
    }
}
